package com.team5101.service.impl;

import com.team5101.dao.IDetailOrderDao;
import com.team5101.dao.IOrderDao;
import com.team5101.entity.AllOrderDTO;
import com.team5101.entity.DetailOrder;
import com.team5101.entity.Order;
import com.team5101.service.IOrderService;
import com.team5101.utils.CreateCodeUtil;
import com.team5101.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*对订单信息的业务逻辑操作*/
@Service
public class OrderServiceImpl implements IOrderService {

    /*基本信息的dao操作*/
    @Autowired
    private IOrderDao orderDao;
    /*详细信息的Dao操作*/
    @Autowired
    private IDetailOrderDao detailOrderDao;

    /*生成一个订单*/
    /*涉及到多条数据的变动，所以需要事务管理*/
    @Transactional
    public R saveAllOrder(AllOrderDTO allOrder){
        /*先获取基本订单的对象信息*/
        Order order =  allOrder.getOrder();
        /*生成一个几乎不重复的订单编号*/
        String orderCode = CreateCodeUtil.CreateOrderCode();
        /*把订单编号放入数据中*/
        order.setOrderCode(orderCode);
        /*创建当前时间的数据*/
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String orderCreateDate = sdf.format(date);
        order.setOrderCreateTime(orderCreateDate);
        /*开始插入基本订单信息的数据，并返一个int类型的数据*/
        int i = orderDao.saveOrder(order);
        /*判断是否插入成功*/
        if(i==1){
            /*获取到订单的详细信息*/
            DetailOrder detailOrder = allOrder.getDetailOrder();
            /*把订单编号放入进去*/
            detailOrder.setOrderCode(orderCode);
            /*开始插入订单详细数据*/
            int index = detailOrderDao.saveDetailOrder(detailOrder);
            /*判断是否插入成功*/
            if (index == 1){
                System.out.println(11111111);
                /*成功就返回成功值，并把orderCode带回去*/
                return R.ok(orderCode);
            }
        }
        return R.error();
    }

    /*获取一个订单的基本信息+详细信息*/
    public R queryAllOrder(String orderCode){
        /*先获取到订单的基本信息*/
        Order order = orderDao.queryOrderByCode(orderCode);
        /*获取到订单的详细信息*/
        DetailOrder detailOrder = detailOrderDao.queryDetailOrderByCode(orderCode);
        /*把获取到的对象放入到所有数据中*/
        AllOrderDTO allOrder = new AllOrderDTO();
        allOrder.setDetailOrder(detailOrder);
        allOrder.setOrder(order);
        /*把数据放入R中返回*/
        return R.ok(allOrder);
    }

    /*查询订单列表信息*/
    public R queryAllOrderList(Order order){
        /*new出一个订单的所有信息*/
        AllOrderDTO allOrder = null;
        /*先new出来一个订单信息的列表*/
        List<AllOrderDTO> allOrders = new ArrayList<AllOrderDTO>();
        /*先根据传过来的订单信息查出基本订单信息*/
        List<Order> orders = orderDao.queryAllOrderByUserId(order);
        /*遍历基本订单信息*/
        for (Order myOrder:orders){
            /*获取当前的订单编号*/
            String orderCode = myOrder.getOrderCode();
            /*查出订单信息对应的订单基本信息*/
            DetailOrder detailOrder = detailOrderDao.queryDetailOrderByCode(orderCode);
            allOrder = new AllOrderDTO();
            /*把订单基本信息和详细信息放入订单所有信息里面*/
            allOrder.setOrder(myOrder);
            allOrder.setDetailOrder(detailOrder);
            /*放入集合里面*/
            allOrders.add(allOrder);
        }

        return R.ok(allOrders);
    }
    /*查询单个订单基本信息*/
    public Order queryOrderByOrderCode(String orderCode) {
        return orderDao.queryOrderByCode(orderCode);
    }

    /*根据订单的编号查出电影数据的信息*/
    public  DetailOrder queryDetailOrderByOrderCode(String orderCode){
        return detailOrderDao.queryDetailOrderByCode(orderCode);
    }
}